package com.pzr.soccer.crawlerHelper;

import com.pzr.soccer.consts.GeneralConst;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.UnknownHostException;
@Slf4j
public class JsoupHelper  {


    private final static int interval = 100;
    private final static int TIME_OUT = 2000;
    private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64";


    public static String getContent(String url) {
        String body="";
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        Document document = null;

        try {
            Connection  conn = Jsoup.connect(url).userAgent(USER_AGENT).timeout(TIME_OUT);
            document = conn.get();
            body=document.body().toString();

        } catch (UnknownHostException netErr) {
            System.out.println("连接错误： " + url);
            body= GeneralConst.Parse_Errors_Occurred+netErr.getMessage();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("parse error： " + url);
            body= GeneralConst.Parse_Errors_Occurred+e.getMessage();

        }

        return body;
    }


}
